doAutoLockProcess = "ZFSS\ZFSS_AutoLock_Functions.sqf";

// Events
_inVehicle_action = -1;



ZFSS_SetAutoLockOnVehicle = {
	private ["_vehicle","_autoLockState"];

	// The vehicle passed by the function call.
	_vehicle = _this select 0;
	
	// AutoLock state passed by function call.
	_autoLockState = _this select 1;
	
	diag_log(format["ZFSS: Vehicle is %1",_vehicle]);
	
	// We only want one of these three states.
	if(_autoLockState == "ENABLED" || _autoLockState == "DISABLED" || _autoLockState == "INACTIVE") then
	{
		_vehicle setVariable ["ZFSS_AutoLock",_autoLockState,false];
	}
	else
	{
		// Garbage input, time to set this as the default.
		_vehicle setVariable ["ZFSS_AutoLock","INACTIVE",false];
	};
};

/*
	Clear the actions for AutoLock for the most recent vehicle (current vehicle)
*/
ZFSS_ClearAutoLockActions = {
	
	// Exit if there's no instance of a user being in a vehicle.
	if(isNull _previousVehicle || isNull _inVehicle_action) exitWith {};
	
	// Get rid of the action from the vehicle so it doesn't appear again.
	_previousVehicle removeAction _inVehicle_action;
	
	// Cache the value from the object we set before. 
	_vehicleAutoLockEnabled = _previousVehicle getVariable "ZFSS_AutoLock";
	
	if(!isNull _vehicleAutoLockEnabled) then
	{
		_previousVehicle setVariable["ZFSS_AutoLock",nil,false];
	};	
};

/*
	Gets the current AutoLock status from the current vehicle
*/
ZFSS_GetAutoLockStatus = {
	private ["_vehicle","_vehicleAutoLockEnabled","_returnValue","_addVehicleAutoLock"];
	
	// Parameter 1 = _vehicle (_previousVehicle)
	_vehicle = _this select 0;
	
	_vehicleAutoLockEnabled = _vehicle getVariable ["ZFSS_AutoLock",["UNSET"]];
	_returnValue = "INACTIVE";
	
	if(typeName _vehicleAutoLockEnabled == "ARRAY") then
	{
		// Set this to INACTIVE since it's not been set.
		_addVehicleAutoLock = [_vehicle,"INACTIVE"] call ZFSS_SetAutoLockOnVehicle;
		_returnValue = _vehicle getVariable "ZFSS_AutoLock";
	}
	else
	{
		_returnValue = _vehicle getVariable "ZFSS_AutoLock";
	};
	
	_returnValue
};

/*
	Gets the text for the action.
*/
ZFSS_GetAutoLockActionText = {
	private ["_vehicle","_autoLockStatus","_outputText"];
	_vehicle = _this select 0;
	
	// Get the AutoLock status of the vehicle.
	_autoLockStatus = [_vehicle] call ZFSS_GetAutoLockStatus;
	_outputText = "Turn AutoLock ON";
	
	if(_autoLockStatus == "ENABLED") then
	{
		_outputText = "Turn AutoLock OFF";
	};
	
	_outputText
	
};

ZFSS_DoUpdateAction ={
	private ["_vehicle","_previousAction","_actionText","_callFunction","_parameterArray"];
	
	_vehicle = _this select 0;
	_previousAction = _this select 1;
	_actionText = _this select 2;
	_callFunction = _this select 3;
	_parameterArray = _this select 4;
	
	// Remove the action first. 
	_vehicle removeAction _previousAction;
	_vehicle removeAction _inVehicle_action;
	
	// Now we add the action
	_inVehicle_action = _vehicle addAction [_actionText,_callFunction,_parameterArray,1.5,false,true];
};




_previousVehicle = objNull;
_playerID = getPlayerUID player;
_autoLockEnabled = false;		// Is AutoLock enabled currently or disabled?
_autoLockStartText = "";

/*
	Need to loop this because the player can get in and out of a vehicle.
*/
while{true} do
{
	// Make sure that when the player gets out of the car, it's 
	if(vehicle player == player) then
	{
		diag_log(format["ZFSS_AutoLock: Player UID %1 AutoLock marker removed. Player is not in a vehicle.",_playerID]);
		_autoLockEnabled = false;
	};
	
	// Now we're ready to repeat, wait until the player is in a vehicle.
	waitUntil{vehicle player != player};
	
	if(vehicle player != player) then
	{
			
			if(isNull _previousVehicle) then
			{
				_previousVehicle = vehicle player;
			};
			
			// Get the text (because we need something to begin with when declaring the action)
			_autoLockStartText = "Toggle AutoLock";
			
			// Then get the status because it could be either inactive or OFF. We need to set the vehicle object properties to know for sure.
			// If the AutoLock state isn't set, GetAutoLockStatus will do it automatically.
			_autoLockStatus = [_previousVehicle] call ZFSS_GetAutoLockStatus;
			
			if(!_autoLockEnabled) then
			{
				// Add the action to the car to start off with, it's 
				[_previousVehicle,_inVehicle_action,_autoLockStartText,doAutoLockProcess,[_autoLockStatus,_previousVehicle,_playerID,_inVehicle_action]] call ZFSS_DoUpdateAction;
			
				_autoLockEnabled = true;
				
				if(ZFSS_Enable_Debug) then
				{
					diag_log(format["ZFSS_AutoLock: Player UID %1 got in vehicle %2, AutoLock Status %3",_playerID,_previousVehicle,_autoLockStatus]);
				};
				
			};
	};
}


//_inVehicle_action = _inVehicle addAction [_autoLockText, doAutoLockProcess,[_inVehicle,_enabledAutoLock]];


